/**
 * 
 */
package com.iptv;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.base.UserGroup;
import com.iptv.operation.IptvUserOperation;

/**
 * @author 4833 robben
 * 2014-5-11 下午4:05:42
 * class to hold user has been login.
 */
public class LoginUserManager {
	// inject thes objects by spring !
	private static IptvUserOperation iptvUserOperation = new IptvUserOperation();
	private static List<String> loginUserCodes = new ArrayList<String>();
	
	/**
	 * remenber user when login
	 * @param code
	 * @return
	 */
	public static boolean remember(String code) {
		if (LoginUserManager.isOnline(code)) {
			return LoginUserManager.loginUserCodes.add(code);
		} else {
			return true;
		}
	}
	
	/**
	 * forget user when logout
	 * @param code
	 * @return
	 */
	public static boolean forget(String code) {
		if (LoginUserManager.isOnline(code)) {
			return LoginUserManager.loginUserCodes.remove(code);
		} else {
			return false;
		}
	}
	
	/**
	 * whether user still live
	 * @param code
	 * @return
	 */
	public static boolean isOnline(String code) {
		Iterator<String> it = LoginUserManager.loginUserCodes.iterator();
		
		while (it.hasNext()) {
			String user = it.next();
			if (code.equals(user)) {
				return true;
			}
		}
		
		return false;
	}
	
	public static IptvUser find(String code) {
//		return (IptvUser) LoginUserManager.iptvUserOperation.findByIdentity(code);
		IptvUser operator = new IptvUser();
		operator.setCode("11111");
		UserGroup ug = new UserGroup();
		ug.setCode("999911");
		operator.setUserGroup(ug);
		return operator;
	}
}
